package hust.ioic.oa.domain;

import java.util.Collection;

import com.opensymphony.xwork2.ActionContext;
// 操作员
public class Operator {
	private Integer id;
	private String username; // 用户名
	private String gender; // 性别
	private String password; // 密码
	private Integer status; // 账户状态
	private Department department; // 部门
	private Role role;
	private Integer isAdmin; // 是否是admin账户，admin账户拥有所有权限

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}

	public Department getDepartment() {
		return department;
	}

	public void setDepartment(Department department) {
		this.department = department;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	public Integer getIsAdmin() {
		return isAdmin;
	}

	public void setIsAdmin(Integer isAdmin) {
		this.isAdmin = isAdmin;
	}

	public boolean hasFunctionByUrl(String privUrl) {
		if (isAdmin == 1) {
			return true;
		}

		// >> 去掉后面的参数
		int pos = privUrl.indexOf("?");
		if (pos > -1) {
			privUrl = privUrl.substring(0, pos);
		}
		// >> 去掉UI后缀
		if (privUrl.endsWith("UI")) {
			privUrl = privUrl.substring(0, privUrl.length() - 2);
		}
		// 如果本URL不需要控制，则登录用户就可以使用
		Collection<String> allFunctionUrls = (Collection<String>) ActionContext
				.getContext().getApplication().get("allFunctionUrls");

		if (!allFunctionUrls.contains(privUrl)) {
			return true;
		} else {

			for (Function priv : role.getFunctions()) {
				if (privUrl.equals(priv.getUrl())) {
					return true;
				}
			}

			return false;
		}
	}

	public boolean hasFunctionByName(String name) {
		if (isAdmin == 1) {
			return true;
		}
		for (Function priv : role.getFunctions()) {
			if (priv.getName().equals(name)) {
				return true;
			}
		}
		return false;
	}

}
